package com.mgface.eth.api;

import com.mgface.eth.tool.Consts;
import com.mgface.eth.tool.Web3JInstance;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.RawTransactionManager;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.StaticGasProvider;

import java.math.BigInteger;
import java.util.Collections;
import java.util.concurrent.ExecutionException;

/**
 * @author wanyuxiang
 * @version 1.0
 * @project eth
 * @desc 合约里面的交易处理
 * @create 2018-06-27 22:30
 **/
public class TxContract extends Contract {

    private static final String BINARY = "60806040526040516020806101508339810180604052810190808051906020019092919050505080600081905550506101138061003d6000396000f3006080604052600436106053576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633bc5de301460585780635b4b73a914608057806373d4a13a14609e575b600080fd5b348015606357600080fd5b50606a60c6565b6040518082815260200191505060405180910390f35b609c6004803603810190808035906020019092919050505060cf565b005b34801560a957600080fd5b5060b060e1565b6040518082815260200191505060405180910390f35b60008054905090565b80600080828254019250508190555050565b600054815600a165627a7a7230582035a255212c88d5119336736c38c4569886edaa29ad26871e44ad9153fcc4e3a80029";

    private TxContract(String contractBinary, String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider gasProvider) {
        super(contractBinary, contractAddress, web3j, transactionManager, gasProvider);
    }

    /**
     *
     * @param methodName 调用合约的方法
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public void getContractValue(String methodName) throws Exception {
        Function function = new Function(methodName,Collections.emptyList(),Collections.singletonList(new TypeReference<Uint256>() {}));
        RemoteCall<Uint256> call = executeRemoteCallSingleValueReturn(function);
        System.out.println(call.send().getValue());
    }

    /**
     *
     * @param methodName  调用的合约方法
     * @param weiValue    给合约传递以太币
     * @throws Exception
     */
    public void setContractValue(String methodName,BigInteger weiValue) throws Exception {
        Function function = new Function(methodName, Collections.singletonList(new Uint256(100)),Collections.emptyList());
        RemoteCall<TransactionReceipt> rxc = executeRemoteCallTransaction(function,weiValue);
        TransactionReceipt receipt = rxc.send();
        System.out.println(receipt);

    }

    public static void main(String[] args) throws Exception {
        Credentials credentials = Credentials.create("8341e5451480e81f220610f9ada693fe7a5dcc19d28833ce0c2f4f4d7f649104");
        TransactionManager transactionManager = new RawTransactionManager(Web3JInstance.getWeb3jInstance(), credentials, Consts.CHAINID);
        StaticGasProvider gasProvider = new StaticGasProvider(Consts.GAS_PRICE,Consts.GAS_LIMIT);
        TxContract tx = new TxContract(BINARY,"0xef297b450200029b4dc96ccd4b25c01c990467b4", Web3JInstance.getWeb3jInstance(),transactionManager,gasProvider);
        tx.getContractValue("getData");
        //tx.setContractValue("setData", Consts.GAS_VALUE);
    }
}
